package com.fm.service;

/**
 * Created by Administrator on 2018/7/30 0030.
 */
import com.fm.entity.Equipment;
import com.fm.entity.EquipmentTopic;
import com.fm.entity.ProductFunction;
import com.fm.mapper.EquipmentTopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EquipmentTopicService extends MBaseService<EquipmentTopic, Integer, EquipmentTopicMapper>{

    @Autowired
    EquipmentService equipmentService;

    @Autowired
    ProductFunctionService productFunctionService;

    public void deleteByEquipment(Integer equipmentId) {
        EquipmentTopic topic = new EquipmentTopic();
        topic.setEquipmentId(equipmentId);
        List<EquipmentTopic> equipmentTopicList = getMyBatisDao().findAll(Example.of(topic));
        for (EquipmentTopic t : equipmentTopicList) {
            getMyBatisDao().delete(t);
        }
    }

    public List<EquipmentTopic> getTopics(Integer id) {
        List<EquipmentTopic> reTopics = new ArrayList<>();
        try{
            if (id == null) return reTopics;
            Equipment equipment = equipmentService.selectOneByExample(id).get();
            EquipmentTopic topic = new EquipmentTopic();
            topic.setType(1);
            topic.setEquipmentId(id);
            List<EquipmentTopic> topics = this.selectByExample(topic);
            //获取所有的事件
            ProductFunction function = new ProductFunction();
            function.setProductId(equipment.getProductId());
            function.setType(3);
            List<ProductFunction> event = productFunctionService.selectByExample(function);
            for (EquipmentTopic t : topics) {
                if (t.getValue().contains("${tsl.event.identifer}")){
                    for (ProductFunction f : event){
                        t.setValue(t.getValue().replace("${tsl.event.identifer}", f.getName()));
                        reTopics.add(t);
                    }
                }else {
                    reTopics.add(t);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return reTopics;
    }
}
